<?php
/**
 * Created by PhpStorm.
 * User: sky
 * Date: 2019/7/16
 * Time: 23:04
 */

namespace App\Repositories;

use App\Models\Members;
use App\Models\Stores;


class StoresRepositories
{
    public function model()
    {
        return Stores::query();
    }

    public function find($id)
    {
        return $this->model()->where('id',$id)->first();
    }

    public function save($data)
    {
        $users = $this->model()->create($data);
        if(isset($data['roles_id']) && !empty($data['roles_id']))
        {
            $rolesId =explode(',',$data['roles_id']);
            $users->manyRoles()->sync($rolesId);
        }

        if(isset($data['permissions_id']) && !empty($data['permissions_id']))
        {
            $permissionsId =explode(',',$data['permissions_id']);
            $users->manyPermissions()->sync($permissionsId);
        }
        return $users;
    }
    public function update($id,$data)
    {
        $updateData=[
            'name'=>$data['name'],
            'mobile'=>$data['mobile'],
            'linkman'=>$data['linkman'],
            'logo'=>$data['logo'],
            'area_id'=>$data['area_id'],
            'address'=>$data['address'],

            'coordinate'=>$data['coordinate'],
            'latitude'=>$data['latitude'],
            'longitude'=>$data['longitude'],

            'summary'=>$data['summary'],
            'status'=>$data['status'],
            'sort'=>$data['sort'],
        ];
        $users =$this->find($id);
        if($users->update($updateData))
        {
            if(isset($data['roles_id']) && !empty($data['roles_id']))
            {
                $rolesId =explode(',',$data['roles_id']);
                $users->manyRoles()->sync($rolesId);
            }

            if(isset($data['permissions_id']) && !empty($data['permissions_id']))
            {
                $permissionsId =explode(',',$data['permissions_id']);
                $users->manyPermissions()->sync($permissionsId);
            }
            return true;
        }

        return false;
    }

    public function search($data)
    {
        $model = $this->model();

        if(isset($data['name']))
        {
            $model = $model->where('name','like','%'.$data['name'].'%');
        }

        //些处扩展更多查询条件
        return $model->orderBy('created_at','desc')->paginate($data['limit'])->toArray();
    }

    public function delete($ids)
    {
        return Stores::destroy($ids);
    }



}